package leetcode.L1000;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Ex981_2 {
    /** Initialize your data structure here. */
    Map<String, TreeMap<Integer,String>> map = new HashMap<>();

    public Ex981_2() {

    }

    public void set(String key, String value, int timestamp) {
        if(map.containsKey(key)){
            TreeMap<Integer, String> tree = map.get(key);
            tree.put(timestamp,value);
        }else{
            TreeMap<Integer, String> newTree = new TreeMap<>();
            newTree.put(timestamp,value);
            map.put(key,newTree);
        }

    }

    public String get(String key, int timestamp) {
        if(map.containsKey(key)){
            TreeMap<Integer, String> tree = map.get(key);
            Integer integer = tree.floorKey(timestamp);
            if(integer!=null){
                String s = tree.getOrDefault(integer,"");
                return s;
            }else{
                return "";
            }

        }else {
            return "";
        }
    }
}
